﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using yiCommerce.Areas.Admin.Models.ProductVariants;
using yiCommerce.Areas.Admin.Models.Products;
using yiCommerce.Attributes;
using yiCommerce.Core.Domain.Products;
using yiCommerce.Service.Products;

namespace yiCommerce.Areas.Admin.Controllers
{

    [AdminAuthorize(Order = 1)]
    [AdminProductAuthorize(Order = 2)]
    public class ProductVariantAttributeController : Controller
    {
        private IProductService _productService;


        public ProductVariantAttributeController(IProductService productService)
        {
            _productService = productService;
        }

        public ActionResult EditAttributeValues(int productVariantAttributeId)
        {
            var pvAttr = _productService.GetProductVariantAttributeByProductVariantAttributeId(productVariantAttributeId);
            ViewBag.Model = new ProductVariantAttributeValueEditModel()
                {
                    PvId = pvAttr.ProductVariantId,
                    ProductVariantAttributeId = productVariantAttributeId,
                    Name = pvAttr.ProductAttribute.Name,
                    Values= pvAttr.ProductVariantAttributeValues.Select(c=>new ProductVariantAttributeValueModel()
                        {
                           Name = c.Name,
                           DisplayOrder = c.DisplayOrder
                        }).ToList()


                };
            return View();
        }

        public ActionResult Create(int productVariantId)
        {

            var allProductAttributes = _productService.GetAllProductAttributes();
            var model = new ProductVariantAttributeModel()
                {
                    PvId = productVariantId,
                    AvailableAttributeControlTypes = ToAttributeControlTypeModels(),
                    AvailableProductAttributes = ToProductAttributeModels(allProductAttributes),
                };
            ViewBag.Model = model;
            return View();
        }

        private static IList<ProductAttributeModel> ToProductAttributeModels(IList<ProductAttribute> allProductAttributes)
        {

            return allProductAttributes.Select(c => new ProductAttributeModel()
                {
                    Id = c.Id,
                    Name = c.Name
                }).ToList();

        }

        private static List<AttributeControlTypeModel> ToAttributeControlTypeModels()
        {
            return Enum.GetValues(typeof(AttributeControlType)).Cast<AttributeControlType>().ToList()
                       .Select(c=>new AttributeControlTypeModel()
                           {
                               ControlTypeName = Enum.GetName(typeof(AttributeControlType), c),
                               ControlTypeValue = (int)c
                           }).ToList();
        }
    }

    public class ProductVariantAttributeValueEditModel 
    {
        public int ProductVariantAttributeId { get; set; }
        public string Name { get; set; }
        public int DisplayOrder { get; set; }

        public int PvId { get; set; }

        public IList<ProductVariantAttributeValueModel> Values { get; set; }
    }
}
